from django.http import HttpResponse
from django.shortcuts import render, get_object_or_404
from .models import Category,Tag,Course
from user.models import MyUser

# Create your views here.
def index(request):
    categorys = Category.objects.all()
    tags = Tag.objects.all()
    courses = Course.objects.all()
    courses_fire = Course.objects.filter(fire=True)[:6]  # 只获取前6个符合条件的课程
    return render(request,'main/index.html',context={
        "categorys":categorys,
        "tags":tags,
        "courses":courses,
        "courses_fire":courses_fire
    })



def course_details(request,course_id):
    course = Course.objects.filter(id=course_id).first()
    return render(request, f'main/course_details.html',context={
        "course":course
    })


def tag_page(request, tag_id):
    tag = Tag.objects.filter(id=tag_id).first()
    courses = tag.course_set.all()
    return render(request,"main/tag_page.html",context={
        "courses":courses,
        "tag":tag,
        "status":True
    })

def category_page(request,category_id,category_id2):
    category = get_object_or_404(Category,id=category_id)
    courses = category.course_set.all()
    return render(request,"main/tag_page.html",context={
        "courses":courses,
        "category":category,
        "status": False
    })